package org.jadapter.tests.spring;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.jadapter.registry.AdapterRegistry;
import org.jadapter.registry.ComponentRegistry;
import org.jadapter.registry.SimpleComponentRegistry;
import org.jadapter.tests.examples.CommaSeparated;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestContainer extends TestCase {

	ApplicationContext ctx;
	

	@Override
	public void setUp() {
		ctx = new ClassPathXmlApplicationContext
			("org/jadapter/tests/spring/context.xml");
	}
	
	
	public void testShouldBeEasyToCreateAdapterRegistryInSpring(){
	    AdapterRegistry adapters = (AdapterRegistry)ctx.getBean("adapters");
	    List<String> list = Arrays.asList("Foo", "Bar");
        
        assertEquals("Foo,Bar", adapters.adapt(CommaSeparated.class, list)
                .getCommaSeparated());    	
        
        Map sequence = adapters.adapt(Map.class, list);
        assertNotNull(sequence);
	    assertEquals(sequence.get(1), "Bar");
	    
	}


	public void testShouldCreateRegistryBean() {
		ctx.getBean("registry");		
	}						
	
	public void testShouldContainOneAdapter(){
		SimpleComponentRegistry registry = 
			(SimpleComponentRegistry)ctx.getBean("registry");
		
		assertEquals(1, registry.size());			
	}
	
	public void testShouldAdaptList() {
		ComponentRegistry registry = 
			(ComponentRegistry)ctx.getBean("registry");
		
		CommaSeparated cs = registry.adapt(CommaSeparated.class, Arrays.asList("Foo", "Bar"));
		        
        String csv = cs.getCommaSeparated();
        assertEquals("csv", "Foo,Bar", csv);		
	}
	
	
}
